package fileshare.fileprocessing;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import fileshare.datastructure.ipinfo;
/**
 * 
 * @author Administrator
 *
 */
public class AcessTextFile {


	public static ipinfo []peerque=new ipinfo[20];
	public static int ipcount=0;
	



	public AcessTextFile()
	{
		
	}
    private static void readToBuffer(StringBuffer buffer, InputStream is)
        throws IOException {
    	int count=0;
        String line;
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        line = reader.readLine();
        
        ipinfo ipp=new ipinfo(line,ipcount+1);
        //peerque 
        peerque[ipcount+1]=new ipinfo(line,ipcount+1);//peer 1 stored into peerque
        ipcount++;
       
        String order=line;

     
        while (line != null) { 

   
            line = reader.readLine();
            if(line==null)
            	break;
            peerque[ipcount+1]=new ipinfo(line,ipcount+1);
            ipcount++;

        }

    }

   
   
    
   public static void readorder()
   {	try{
	   InputStream is = new FileInputStream("CEN5501C//PeerInfo.cfg");
       StringBuffer buffer = new StringBuffer();
       readToBuffer(buffer, is);
    
       is.close();
       
        }catch(Exception e){e.printStackTrace();
        String loge=e.getLocalizedMessage();
        	String loge2=e.getStackTrace().toString();
        
        log.writelog("aa"+loge+"dd"+loge2);
        
        }
   }
   public ipinfo getipinfo(String tpeerid)// using the peerid to get your own ip information
   {
	   for(int i=1;i<ipcount+1;i++)
	   {
		   if(peerque[i].peeridinfo.equals(tpeerid))
		   {
			   
			   return peerque[i];
			   
		   }
	   }
	   return null;
	   
   }
   /**
    * 
    * @param peerid
    * @return the index of the peer checked in the form of 1,2,3
    */
   public static int id2index(int peerid)//check which peer it is using the peerid
   {
	   for(int i=1;i<ipcount+1;i++)
	   {
//		   
		   if(Integer.parseInt(peerque[i].peeridinfo)==peerid)
			   return peerque[i].index;
	   }
	   return -2;
	   
   }
   // try to get a certain peer ip information based on its index related to the self peer
   public ipinfo getipinfobyindex(int t_index)// using the peerid to get your own ip information
   {
	   if(t_index>ipcount)
		   System.out.println("getipinfo erro");
	    return peerque[t_index];
   }
   public ipinfo getipinfobyhostnameport (String hn)// using the peerid to get your own ip information
   {
	   for(int i=1;i<ipcount+1;i++)
	   {
		   if(peerque[i].ipaddress.equals(hn))
		   {
			   
			   return peerque[i];
			   
		   }
	   }
	   return null;
	   
   }
   
  
  

}